// 格式化数字（点赞、评论、订阅者等）
export function formatNumber(num) {
    if (num === null || num === undefined) return 0;
    if (num >= 100000000) {
        return (num / 100000000).toFixed(1).replace(/\.0$/, '') + '亿';
    }
    if (num >= 10000) {
        return (num / 10000).toFixed(1).replace(/\.0$/, '') + '万';
    }
    if (num >= 1000) {
        // 对于 1000 到 9999 的数字，可以选择显示具体数字或 K
        // return (num / 1000).toFixed(1).replace(/\.0$/, '') + 'K';
        return num.toLocaleString(); // 显示具体数字，带千位分隔符
    }
    return num;
}

// 格式化观看次数
export function formatViews(views) {
    return formatNumber(views);
}

// 格式化订阅者数量
export function formatSubscribers(count) {
    return formatNumber(count);
}

// 格式化日期（相对时间）
export function formatDate(dateString) {
    if (!dateString) return '';
    const date = new Date(dateString);
    const now = new Date();
    const diff = now.getTime() - date.getTime();

    const seconds = Math.floor(diff / 1000);
    const minutes = Math.floor(seconds / 60);
    const hours = Math.floor(minutes / 60);
    const days = Math.floor(hours / 24);
    const weeks = Math.floor(days / 7);
    const months = Math.floor(days / 30);
    const years = Math.floor(days / 365);

    if (years > 0) {
        return `${years}年前`;
    }
    if (months > 0) {
        return `${months}个月前`;
    }
    if (weeks > 0) {
        return `${weeks}周前`;
    }
    if (days > 0) {
        return `${days}天前`;
    }
    if (hours > 0) {
        return `${hours}小时前`;
    }
    if (minutes > 0) {
        return `${minutes}分钟前`;
    }
    if (seconds < 10) {
        return '刚刚';
    }
    return `${seconds}秒前`;
}

// 格式化视频时长 (秒 -> HH:MM:SS 或 MM:SS)
export function formatDuration(seconds) {
    if (seconds === null || seconds === undefined || isNaN(seconds) || seconds < 0) return '0:00';
    seconds = Math.round(seconds);
    const hours = Math.floor(seconds / 3600);
    const minutes = Math.floor((seconds % 3600) / 60);
    const remainingSeconds = seconds % 60;

    if (hours > 0) {
        return `${hours}:${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}`;
    }
    return `${minutes}:${remainingSeconds.toString().padStart(2, '0')}`;
}